<!-- 公共组件页面 -->
<template>
	<view class="publicPage">
		<view v-if="isNavbar">
			<slot name="navbar"></slot>
		</view>
		<!-- 内容高度 -->
		<view :style="bodyStyle">
			<slot></slot>
		</view>

		<u-popup :show="showPrivacy" mode="bottom" :round="10" :safeAreaInsetBottom="false" :zIndex="20000" :overlayStyle="{'zIndex': 19999}">
			<view class="px-20">
				<view class="tc fw-7 fz-15 py-10" style="letter-spacing: 2rpx">隐私权限声明</view>
				<view class="py-10" style="letter-spacing: 2rpx"
					>欢迎您使用一键视界小程序,您可使用本小程序以实现在线支付、咨询专家、查看订单等功能。我们将严格遵守相关法律法规和隐私政策以保护您的个人信息。为提供基本服务,需要联网以及调用您的部分权限或功能。</view
				>
				<view style="letter-spacing: 2rpx">请您阅读并同意<text class="colorZS" @click="openPrivacyContract">《用户隐私保护指引》</text></view>
				<view class="disFlxJS pt-20 pb-30">
          <navigator target="miniProgram" open-type="exit">
            <button plain class="closeBtn fz-14 fw-7" style="letter-spacing: 2rpx">退出</button>
          </navigator>
					<button
            id="agree-btn"
						plain
						class="agreeBtn fz-14 fw-7"
						style="letter-spacing: 2rpx"
						open-type="agreePrivacyAuthorization"
						@agreeprivacyauthorization="handleAgree"
					>
						同意并继续
					</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
export default {
	props: {
		// 是否显示自定义tabbar, 原生tabbar不用减去高度,暂未使用
		isTabbar: {
			type: Boolean,
			default: false,
		},
		//  是否自定义导航
		//  true会为主盒子自动减去导航高度,避免每个页面再重复计算
		//  false则自己去配置小程序原生的导航,性能更好
		isNavbar: {
			type: Boolean,
			default: false,
		},
		//  是否需要悬浮扫一扫
		isScanIt: {
			type: Boolean,
			default: false,
		},
		bgColor: {
			type: String,
			default: "#F5F5F5",
		},
		// 是否需要监听软键盘高度,设置此属性的同时,请将input的adjust-position设置为false
		isMinusKeyHeight: {
			type: Boolean,
			default: false,
		},
	},
	computed: {
		// 当前选中tabbar
		currentTabbar() {
			return this.$store.state.activeTabbar;
		},
		// 主盒子样式
		bodyStyle() {
			return `height: calc(100vh - ${
				this.isNavbar ? this.$store.state.deviceInfo.navbarHeight + this.keyboardHeight : 0
			}px - ${
				this.isTabbar ? this.$store.state.deviceInfo.tabbarHeight : 0
			}px);overflow: hidden auto;background-color: ${this.bgColor};`;
		},
    // 隐私协议弹窗
    showPrivacy() {
      return this.$store.state.showPrivacy
    }
	},
	data() {
		return {
			keyboardHeight: 0,
		};
	},
	created() {
    uni.hideTabBar();

		if (this.isMinusKeyHeight) {
			uni.onKeyboardHeightChange((res) => {
				this.keyboardHeight = res.height || 0;
			});
		} else {
			uni.offKeyboardHeightChange();
		}

    this.handleAgree(); 
	},
	methods: {
    // 判断是否已同意隐私协议
		handleAgree() {
      if (uni.getPrivacySetting) {
				const that = this;
				uni.getPrivacySetting({
					success: (res) => {
            that.$store.commit('SET_SHOWPRIVACY', res.needAuthorization);
						console.log("是否需要授权：", res.needAuthorization, res.privacyContractName);

            if(!res.needAuthorization){
              setTimeout(() => {
                uni.showTabBar();
              }, 300);
            }
					},
          fail: () => {
            uni.showTabBar();
          }
				});
			} else {
        uni.showTabBar();
				console.log("低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用");
			}
		},
		// 打开隐私协议
		openPrivacyContract() {
			const that = this;
			uni.openPrivacyContract({
				success: (res) => {
					console.log(`打开隐私协议`);
				},
				fail: (res) => {
					console.error(`打开隐私协议失败`, res);
				},
			});
		}
	},
};
</script>
<style lang="scss" scoped>
.closeBtn,
.agreeBtn {
	background-color: #f5f5f5;
	border-radius: 50rpx;
	height: 80rpx;
	line-height: 80rpx;
	padding: 0 100rpx;
	border: none;
	color: #666;
  margin: 0;
}
.agreeBtn {
	color: #20d19a;
}
</style>
